const BANNER_URL="http://localhost/weixin/caizhuangshop/listBanner.php";
const NAV_URL="http://localhost/weixin/caizhuangshop/listType.php";
const SERVICE_URL="http://localhost//weixin/caizhuangshop/listService.php";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner:null,
    interval:2000,
    duration:1000,
    navItems:null,
    curNavId:1,
    curNavName:'推荐',
    start:0,
    pageSize:3,
    count:0,
    list:null
  },

  getBanner:function(){
    var that=this;
    wx.request({
      url: BANNER_URL,
      success:function(res){
        console.log(res);
        that.setData({
          banner:res.data.banner
        })
      }
    })
  },

  getNavData:function(){
    var that=this;
    wx.request({
      url: NAV_URL,
      success:function(res){
        console.log(res);
        that.setData({
          navItems:res.data.navItems
        })
      }
    })
  },

  switchType:function(e){
    //console.log(e);
    var id=e.currentTarget.dataset.id;
    var name=e.currentTarget.dataset.name;
    this.setData({
      curNavId:id,
      curNavName:name
    });
    this.loadListData(id,0);
  },

  loadListData:function(type_id,start,isLoadmore){
    var isLoadmore=arguments[2];
    var that=this;
    wx.showLoading({
      title: '加载中...',
    });
    wx.request({
      url: SERVICE_URL,
      data:{
        start:start,
        pageSize:that.data.pageSize,
        type_id:type_id
      },
      success:function(res){
       // console.log(res);
       var allData="";
       if(isLoadmore){
         allData=that.data.list.concat(res.data.list);
       }else{
         allData=res.data.list;
       }
       that.setData({
         list:allData,
         start:start+that.data.pageSize,
         count:res.data.totalCount

       })
      },
      complete:function(res){
        wx.hideLoading();
      }
    })
  },

  loadMore:function(){
    if(this.data.start>this.data.count-1){
      wx.showToast({
        title: '没有数据了',
      })
      return ;
    }
    var type_id=1;
    if(this.data.curNavName!='推荐'){
      type_id=this.data.curNavId;
    }
    this.loadListData(type_id,this.data.start,true);
  },

  navigateDetail:function(e){
    console.log(e)
    wx.navigateTo({
      url: '../detail/detail?id='+e.currentTarget.dataset.id,
    })
  },

  book:function(e){
    console.log(e);
    wx.redirectTo({
      url: '../book/book?id='+e.currentTarget.dataset.id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBanner();
    this.getNavData();
    this.loadListData(1,0);
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})